package com.yyh.stu;

import java.util.ArrayList;
import java.util.Objects;

/*使用集合存储多个狗狗的信息（至少4个），狗狗类的属性昵称、品种和健康值。
完成：
1.创建多个狗狗对象
2.创建ArrayList集合对象，并把多个狗狗对象放入其中
3.通过循环遍历集合统计健康值低于60的狗狗数量，并输出狗狗昵称及对应的健康值*/
public class Test1 {
    public static void main(String[] args) {
        //创建多个狗狗对象
        Dog dog1 = new Dog("小黄", "秋田犬", 90);
        Dog dog2 = new Dog("小黑", "哈士奇", 80);
        Dog dog3 = new Dog("小王", "rottweiler", 70);
        Dog dog4 = new Dog("小张", "rottweiler", 50);
        //创建ArrayList集合对象
        ArrayList<Dog> list = new ArrayList<>();
        //把多个狗狗对象放入集合
        list.add(dog1);
        list.add(dog2);
        list.add(dog3);
        list.add(dog4);
        //通过循环遍历集合
        //定义数量变量
        int count = 0;
        for (Dog dog : list) {
            //判断
            if (dog.getHealth() < 60) {
                System.out.println("狗狗昵称："+dog.getNickName() + "，健康值：" + dog.getHealth());
                count++;
            }
        }
        System.out.println("共有" + count + "只狗狗健康值低于60");
    }
}
//创建狗狗类
class Dog{
    //属性
    //昵称
    private String nickName;
    //品种
    private String variety;
    //健康值
    private int health;
    //无参构造方法
    public Dog() {
    }
    //有参构造方法
    public Dog(String nickName, String variety, int health) {
        this.nickName = nickName;
        this.variety = variety;
        this.health = health;
    }
    //getter 和 setter 方法
    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public String getVariety() {
        return variety;
    }

    public void setVariety(String variety) {
        this.variety = variety;
    }

    public int getHealth() {
        return health;
    }

    public void setHealth(int health) {
        this.health = health;
    }
    //重写equals和hashCode方法
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Dog dog = (Dog) o;
        return health == dog.health && Objects.equals(nickName, dog.nickName) && Objects.equals(variety, dog.variety);
    }

    @Override
    public int hashCode() {
        return Objects.hash(nickName, variety, health);
    }
    //toString方法
    @Override
    public String toString() {
        return "Dog{" +
                "nickName='" + nickName + '\'' +
                ", variety='" + variety + '\'' +
                ", health=" + health +
                '}';
    }
}
